package com.example.second.queue;

import com.alibaba.fastjson.JSONObject;
import com.example.second.service.IStorageService;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

@Component
public class StorageQueue {

    @Autowired
    private IStorageService iStorageService;
    //用于监听库存队列，减库存
    @RabbitListener(queues = "storage_queue")
    public void insertStorage(String msg){

        Map<String,Object>  ordermsgmap = JSONObject.parseObject(msg,Map.class);
        Map<String,Object> resultMap = new HashMap<String, Object>();
        resultMap = iStorageService.insertStorge(ordermsgmap.get("sku_id").toString(), 0, 1);
        if(!(Boolean) resultMap.get("result")){
            System.out.println(msg+"减库存失败");
        }
        String orderid = ordermsgmap.get("order_id").toString();
        //这个判断用于压力测试
        if (orderid.startsWith(" ")){
            orderid = orderid.substring(1);
        }
        boolean update =(boolean)iStorageService.updateOrderStatus(orderid).get("result");
        if(!update){
            System.out.println("更新订单状态失败");
        }
    }
}
